<?php
namespace app\common\model;


use app\common\util\Jwt;
use app\common\util\Mobile;
use app\common\util\MyAuth;
use app\common\util\Password;

use app\common\util\Account;

use app\common\util\SmsCode\impl\TestSmsCodeCodeImpl;
use think\db\exception\DbException;
use think\wenhainan\Auth;

class UserModel extends CommonModel
{
    protected $name = "user";
    public static function build_payload($uid){
        $ins=new MyAuth();
        return [
            "uid"=>$uid,
            "userInfo"=>$ins->getUserInfo($uid),
            "role"=>$ins->getRole($uid),
            "groups"=>$ins->getGroups($uid),
            "authList"=>$ins->getAuthList($uid,config("auth.auth_type")),
        ];
    }
    public function login_do($account, $password)
    {
        $where = [];
        $t = Account::$accountField."|".Mobile::$mobileField;
        $where[] = [$t, '=', $account];
        $user = $this->where($where)->find();
        if (!$user) {
            $this->setErr("用户不存在");
            return false;
        }
        $storedPass = $user["password"];
        $salt = $user['salt'];
        $checkPass = Password::verify_password($password, $storedPass, $salt);
        if (!$checkPass) {
            $this->setErr("密码错误");
            return false;
        }
        $payload =  self::build_payload($user['id']);
        $jwt = Jwt::generateJWT($payload);

        cache($jwt, json_encode($payload), 3600);
        return [
            'uid'=>$user['id'],
            'jwt' =>$jwt
        ];
    }
    public function reg_do_by_account($account, $password)
    {
        if (!Account::min_len_limit($account,6)){
            $this->setErr("用户名至少6位");
            return false;
        }
        try {
            $data[Account::$accountField] = $account;
            $data['salt'] = Password::gen_salt();
            $data['password'] = Password::gen_pass_hash($password,$data['salt']);
            $data['create_time']=time();
            $uid = $this->insert($data,true);
            if (!$uid)throw new DbException("注册失败");
            return self::login_do($account,$password);
        }catch (DbException $exception){
            $this->setErr($exception->getMessage());
            return false;
        }

    }
    public function reg_do_by_mobile($mobile,$password,$smsSn,$smsCode)
    {
        $is_chinese_mobile = Mobile::is_chinese_mobile($mobile);
        if (!$is_chinese_mobile){
            $this->setErr("手机号码不正确");
            return false;
        }

        $smsCheckIsOk = TestSmsCodeCodeImpl::checkIsOk($smsSn, $smsCode,"reg");
        if (!$smsCheckIsOk){
            $this->setErr("短信码不正确");
            return false;
        }

        try {
            $data[Account::$accountField]=Account::genRandOne();
            $data[Mobile::$mobileField] = $mobile;
            $data['salt'] = Password::gen_salt();
            $data['password'] = Password::gen_pass_hash($password,$data['salt']);
            $data['create_time']=time();
            $uid = $this->insert($data,true);
            if (!$uid)throw new DbException("注册失败");
            return self::login_do($data[Account::$accountField],$password);
        }catch (DbException $exception){
            $this->setErr($exception->getMessage());
            return false;
        }
    }
}
 